package tutorial.lite_mod.favorite_things;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.apache.log4j.Logger;
import public_util.model.*;
import public_util.servlet.ServletParent;
import system_api_4_trade.util.tool_4_handle.StringToolApi;
import system_api_4_trade.util.tool_4_handle.Tool_4_ServletApi;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * 工具类放这里方便复制
 */
class Tutorial4LiteModTool {


    private static Logger logger = Logger.getLogger(Tutorial4LiteModTool.class);


    public static ReturnData show_stat_4_goods_rank(SqlSessionFactory sqlSession, ServletParent thisParent, HttpSession hsession, HttpServletResponse response, HttpServletRequest request){

        logger.info("----show_stat_4_class----");
        ReturnData resultData = new ReturnData();
        PublicDataAdminUser adminUser = (PublicDataAdminUser) Tool_4_ServletApi.getAttribute(null, hsession, Const4PublicDataBase.MEM_4_self_public_data_admin_user);
        PublicAgentInfo agentInfo = (PublicAgentInfo)Tool_4_ServletApi.getAttribute(null, hsession,Const4PublicDataBase.MEM_4_self_public_agent_info);
        resultData = Tool_4_ServletApi.checkAccessType(hsession, resultData, 102);
        if (resultData.getStatus() != 0)
        {
            return resultData;
        }

        String id_admin = "-1";

        if (agentInfo != null)
        {
            id_admin = agentInfo.getId_public_data_admin_user() + "";
        }

        if (adminUser != null)
        {
            id_admin = adminUser.getId_public_data_admin_user() + "";
        }

        String begin_time = request.getParameter(Const4PublicDataBase.PARA_begin_time);
        String end_time = request.getParameter(Const4PublicDataBase.PARA_end_time);
        String id_public_user_info = request.getParameter(Const4PublicDataBase.PARA_id_public_user_info);
        id_public_user_info = StringToolApi.paraHttpFilter(id_public_user_info);


        String length_4_list = request.getParameter(Const4PublicDataBase.PARA_length_4_list);
        if (length_4_list == null)
        {
            resultData.setMsg("length_4_list未传入");
            return resultData;
        }


        SqlSession session = sqlSession.openSession();
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Transaction newTransaction = transactionFactory.newTransaction(session.getConnection(), false);

        Tutorial4LiteModMapper tlMopper = session.getMapper(Tutorial4LiteModMapper.class);//小模式
        try {


            HashMap<String, Object> para4inOut = new HashMap<String, Object>();
            para4inOut.put(Const4PublicDataBase.PARA_id_public_user_info, id_public_user_info);
            para4inOut.put(Const4PublicDataBase.PARA_is_online, 0);
            para4inOut.put(Const4PublicDataBase.PARA_begin_time, begin_time);
            para4inOut.put(Const4PublicDataBase.PARA_end_time, end_time);
            para4inOut.put(Const4PublicDataBase.PARA_length_4_list, length_4_list);
            List<PublicMiddleTable> result_all = tlMopper.show_stat_4_goods_rank(para4inOut);


            HashMap<String, Object> returnData = new HashMap<String, Object>();
            returnData.put("class_data", result_all);
            resultData.setStatus(0);
            resultData.setReturnData(returnData);
            return resultData;
        }
        catch(Exception e)
        {
            try {
                newTransaction.rollback();
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;

            } catch (SQLException e1) {
                String msg = Tool_4_ServletApi.showStackTrace(e);
                resultData.setMsg(msg);
                return resultData;
            }
        }
        finally
        {
            session.close();
        }
    }


}